<?php
ArrowController::importModel("org.arrowplatform.model.IArrowModelField");
abstract class ArrowAbstractTableColumn extends ArrowTagContainer implements IArrowModelField{
	
	protected $mapping = false;
	protected $class = "";
	protected $field = false;
	private $imagesMap = array();
	private $tooltip = null;
	private $defautlImage = "";
	
	public function configure(){
		$this->addProperties(array(
			"name" => null,
			"field" => null,
			"sort" => null,
			"sortable" => 1,
			"width" => false,
			"type" => false,
			"text" => false,
			"mapping" => false,
			"title" => false,
			"class" => "",
			"enabled" => 1,
			"alias" => false,
			"sqlFunction" => false
		));
		$this->addRequiredProperties(array("field", "name"));
		$this->setRequiredParent("ArrowTable");
		$this->setDisplay(false);
		
	}
	
	public function init(){
		$this->field = $this->getProperty("field");
		
		$mapping = $this->getProperty("mapping");
		if( $mapping )
			$this->mapping = $this->getProperty("mapping");
			
		
			
		$imagesMap = $this->getChildren("ArrowMapImage");
		foreach($imagesMap as $map){
			$field = $map->getProperty("field");
			if(empty($field)){
				$this->defautlImage = $map->getImagePath();
			}else{
				
				$this->imagesMap[$field][$map->getProperty("value")] = $map->getImagePath();
			}
		}
		$table = $this->getParent("ArrowTable");
		$class = $this->getProperty("class");
		if( $table->getSortField()==$this->field)
			$class.= " ctrl-table-cell-sorted ";
			
		$this->tooltip = $this->getFirstChild("ArrowCtrlTooltip");
		
			
			
		if( !empty($class) ) $this->class = 'class="'.$class.'"';
	}
	
	public function generateHeader(){
		$width = $this->getProperty("width");
		if($width == -1)
			return "";
		
		$table = $this->getParent("ArrowTable");
		
		$field = $this->getProperty("sort")?$this->getProperty("sort"):$this->field;
		
		$dir = "asc";
		if( $table->getSortField()==$field
			&& 
			$table->getSortDir() == $dir
		){
			$dir = "desc";
		}
		
		
		$style = $width?"style=\"width:".$width."px;\"":"";
		
		
		$str = '<th '.$style.' class="'.self::CTRL_NAMESPACE.'-table-column-head '.self::CTRL_NAMESPACE.'-table-column-head-'.str_replace( array(":","[","]"), array( "_", "_", "_" ), $this->getProperty("field")).'" ><div style="position: relative;">';
		
		if( $this->isSortable() && $table->isSortable() ){
			$state = ArrowCtrlStateManager::getDefault();
			$href = $state->getStateChangeLink( $table, array( 
				"sort" => $field, 
				"sort-dir" => $dir  
			));
			$class = "";
			if( $table->getSortField()==$field) 
				$class= 'class="'.self::CTRL_NAMESPACE.'-table-column-sort-'.$dir.'"';
			$title= "" ;
			$titlevar = $this->getProperty("title") ;
			if( $titlevar != false )	$title = "title=\"$titlevar\"" ;
			$str.='<a '.$title.' '.$class.' href="'.$href.'">'.$this->getName()."</a>";
		}else{
			$str.= $this->getName();
		}
		
		if($filter = $this->getFirstChild("ArrowAbstractTableFilter")){
			$str.= $filter->generate();
		}
		
		return $str."</div></th>";
	}
	
	public function generateSetFilters() {
		
		if($filter = $this->getFirstChild("ArrowAbstractTableFilter")) {
			$tmp = $this->getProperty("title");
			
			if(!empty($tmp))
				return $filter->generateSetFilter($tmp);
			else
				return $filter->generateSetFilter($this->getProperty("name"));
		}
	}
	
	protected function getImage($context, $pathOnly = false){
		$image = "";
		if(!empty($this->imagesMap)){
			foreach($this->imagesMap as $field => $val){
				if( $field )
					if(isset($this->imagesMap[$field][$context->getValue($field)]))
						$image = $this->imagesMap[$field][$context->getValue($field)];
			}
		}
		if(!$image && $this->defautlImage)
			$image = $this->defautlImage;
			
		if($pathOnly)
			return $image;
		if($image)
			$image = '<img src="'.$image.'" class="ctrl-table-map-image" /> ';
		return $image;
	}

	public function generateCell( $context ){
		if($this->tooltip != null){
			$tmp = $this->generateRowCell($context);
			return substr( $tmp, 0, -5).$this->tooltip->generateTooltip($context)."</td>";;
		}
		
		return $this->generateRowCell($context);
	}
	
	protected abstract function generateRowCell($context);
	
	public function getName(){
		return $this->getProperty("name");
	}
	
	public function getField(){
		return $this->field;
	}
	
	

	public function isSortable(){
		return $this->getProperty("sortable");
	}
	
	public function getSort(){
		return $this->getProperty("sort");
	}
	
	public function isAllowed(){
		return true;
	}
	
	public function generateOutputCode(){
		return " ";
	}
}

?>